/*

Slave client, will be made cross platform as it comes together
this makes testing easier.

By ELChupathingy

*/

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "packet_manager.h"
#include "heart_beat.h"
#include "authenticate.h"
#include "heart_beat_filter.h"
#include "sink_filter.h"

#define BOOST_TEST_MODULE "Slave Tests"
#include <boost/test/included/unit_test.hpp>


BOOST_AUTO_TEST_CASE( Packet_Class_Test )
{
	printf( "\nPacket Class Test\n" );
	heart_beat h;
	h.init( 0, 10, 10, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );

	h.print();

	authenticate a;
	a.init( 0, 10, 10, 113, 13, 1337 );

	a.print();
}

BOOST_AUTO_TEST_CASE( Memory_Pool_Packet_Allocate_Test )
{
	printf( "\nMemory_Pool_Packet_Allocate_Test\n" );

	packet_manager *m = new packet_manager( 10 );

	m->print();

	delete m;
}

BOOST_AUTO_TEST_CASE( Packet_Manager_Parse_Test )
{
	printf( "\nPacket_Manager_Parse_Test, buffer allocations done via buffer_pool\n" );


	authenticate a;
	a.init( 0, 10, 10, 1337, 13, 37 );

	packet_manager *m = new packet_manager( 5000 );

	packet *p = m->parse( (unsigned char *)a.serialize(), 200 );

	BOOST_CHECK_EQUAL( p->getSrc(), 10 );
	BOOST_CHECK_EQUAL( p->getDst(), 10 );

	p->print();

	heart_beat h;
	h.init( 0, 10, 10, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );

	p = m->parse( (unsigned char *)h.serialize(), 200 );

	BOOST_CHECK_EQUAL( p->getSrc(), 10 );
	BOOST_CHECK_EQUAL( p->getDst(), 10 );

	BOOST_CHECK( strcmp( ((heart_beat *)p)->getId(), "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" ) == 0 );

	p->print();

	printf( "\nPacket memory stats\n" );
	p->memory_stats();

	printf( "\nPacket Manager Stats\n" );
	m->print();

	delete m;
}

BOOST_AUTO_TEST_CASE( Packet_Filter_Test )
{
	printf( "\nPacket_Filter_Test\n" );

	heart_beat_filter *hbf = new heart_beat_filter();
	sink_filter *sink = new sink_filter();

	hbf->chain( sink );

	heart_beat h;
	h.init( 0, 10, 10, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );

	packet_manager m( 10 );

	hbf->process( m.parse( (unsigned char *)h.serialize(), heart_beat::PACKET_HEARTBEAT_SIZE + packet::PACKET_HEADER_SIZE ) );

	authenticate a;
	a.init( 0, 10, 10, 113, 13, 1337 );

	hbf->process( m.parse( (unsigned char *)a.serialize(), authenticate::PACKET_AUTHENTICATE_SIZE + packet::PACKET_HEADER_SIZE ) );

	delete hbf;
	delete sink;
}